package fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.PersonEntity;

import dbdao.UserDBHepler;
import utils.texttools.RegexValidateUtil;
import utils.texttools.StringUtil;

/**
 * Created by dllo on 15/12/2.
 */
public class RegisteredFragment extends AbsBaseFragment implements View.OnClickListener {
    private Button determineBtn;
    private EditText passwordET, nameET;
    private UserDBHepler hepler;
    private ImageView titleForAllBack;
    private TextView titleForAllTv;
//    private CharSequence temp;//监听前的文本
//    private int editStart;//光标开始位置
//    private int editEnd;//光标结束位置
//    private final int charMaxNum = 15;
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_registered;
    }

    @Override
    protected void initView() {
        titleForAllBack = findView(R.id.titleForAllBack);
        determineBtn = findView(R.id.determineBtn);
        passwordET = findView(R.id.passwordET);
        nameET = findView(R.id.nameET);
        titleForAllTv = findView(R.id.titleForAllTv);
//        nameSizeTv =findView(R.id.nameSizeTV);
    }

    @Override
    protected void initData() {
        titleForAllTv.setText("注册");
        nameET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                nameSizeTv.setText("还能输入" + (charMaxNum - s.length()) + "字符");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    determineBtn.setEnabled(true); //使能控件，如果设置为false，该控件永远不会活动，不管设置为什么属性，都无效；
                    determineBtn.setClickable(true);//设置为true时，表明控件可以点击，如果为false，就不能点击；
                    determineBtn.setBackgroundResource(R.drawable.tab_login_btn);
                } else {
                    determineBtn.setClickable(false);
                    determineBtn.setClickable(false);
                    determineBtn.setBackgroundColor(0xFF9b9694);
                }

//                editStart = nameET.getSelectionStart();
//                editEnd = nameET.getSelectionEnd();
//                if (temp.length() > charMaxNum) {
//                    showCustomToast("你输入的字数已经超过了限制！",R.mipmap.ic_launcher);
//                    s.delete(editStart - 1, editEnd);
//                    int tempSelection = editStart;
//                    nameET.setText(s);
//                    nameET.setSelection(tempSelection);
//                }
            }
        });
        determineBtn.setOnClickListener(this);
        hepler = UserDBHepler.getInstance();
        titleForAllBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.determineBtn:
                String password = passwordET.getText().toString();
                String name = nameET.getText().toString();
                if (StringUtil.isEmpty(name)) {
                    toastWarning("用户名不能为空");
                } else {
                    if (StringUtil.isEmpty(password)) {
                        toastWarning("密码不能为空");
                    } else {
                        if (RegexValidateUtil.checkEmail(name) || RegexValidateUtil.checkMobileNumber(name)) {
                            PersonEntity usermsg = hepler.getUsermsgByName(name);
                            if (usermsg == null) {
                                PersonEntity user = new PersonEntity();
                                user.setPassword(password);
                                user.setPhoneNumber(name);
                                hepler.addUser(user);
                                toastPrompt("注册成功");

                                goBack();
                            } else {
                                toastWarning("该用户已存在");
                            }
                        }else {
                            toastWarning("用户名只能为邮箱或者手机号码");
                        }
                    }
                }
                break;
        }
    }
}
